﻿package game.account.header {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	import game.ui.Text;
	import game.ui.Button;
	import game.ui.DynamicClip;
	import game.objects.DynamicObject;
	import game.ui.Slider;
	import game.Config;
	import flash.media.Camera;
	import game.ui.Money;
	
	public class Header extends DynamicClip {
		
		private function get slider():Slider		{ return getChildByName('slider')			as Slider;	}
		private function get btnLeftStep():Button	{ return getChildByName('btnLeftStep')		as Button;	}
		private function get btnLeftSteps():Button	{ return getChildByName('btnLeftSteps')		as Button;	}
		private function get btnRightStep():Button	{ return getChildByName('btnRightStep')		as Button;	}
		private function get btnRightSteps():Button	{ return getChildByName('btnRightSteps')	as Button;	}
		
		public var donat:Money;
		public var money:Money;
		public var exp:Exp;
		
		public function Header():void {
			var skin:DynamicObject = config.getSkin(DynamicObject.HEADER);
			
			super(skin);
			
			if (!skin) return;
			
			if (!btnLeftStep || !btnLeftSteps || !btnRightStep || !btnRightSteps) return;
			
			btnLeftStep.scaleX = 
			btnLeftStep.scaleY = 
			btnLeftSteps.scaleX = 
			btnLeftSteps.scaleY = 
			btnRightStep.scaleX = 
			btnRightStep.scaleY = 
			btnRightSteps.scaleX = 
			btnRightSteps.scaleY = 
			0.7;
			
			btnLeftStep.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(-1);			});
			btnLeftSteps.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(-slider.length);	});
			btnRightStep.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(1);			});
			btnRightSteps.addEventListener(MouseEvent.CLICK, function (event:Event)	{ step(slider.length);		});
			
			money = new Money ( Money.MONEY, config.me.money, Money.RIGHT );
			donat = new Money ( Money.DONAT, config.me.donat, Money.RIGHT );
			money.x = 10;
			donat.x = 140;
			money.y = donat.y = 9;
			addChild(money);
			addChild(donat);
			
			money.name = 'money';
			donat.name = 'donat';
			
			
			exp = new Exp;
			exp.x = 15;
			exp.y = 35;
			addChild(exp);
			
			createHSlider();
		}
		
		private function createHSlider():void {
			var c:Class;
			for each (var b:String in config.hslider) {
				c = getDefinitionByName(Config.BUTTONS_CLASS_DIR+b) as Class;
				if (!c) continue;
				var button:Button = new c;
				button.name = b;
				button.addEventListener ( MouseEvent.CLICK, hsliderHandler );
				slider.add(button);
			}
		}
		
		private function hsliderHandler(event:MouseEvent):void { 
			this.dispatchEvent( new HSliderEvent ( event.target.name ) );
		}
		
		public function step(value:int):void {
			if (slider)
				slider.step(value);
		}
		
	}
	
}